/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.courses;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

import fr.ove.openmath.OpenMathizable;
import fr.ove.openmath.jome.Jome;
import fr.ove.openmath.courses.editor.BasicEditor;
import fr.ove.openmath.courses.editor.events.*;
import fr.ove.applet.*;
import fr.ove.applet.events.*;


public class EditableJomeApplet extends Applet implements EditorListener, OpenMathizable {
    Jome jome;
    BasicEditor editor;
    
	public void init() {
		setLayout(new BorderLayout(0,0));
		setBackground(Color.white);
				
		Panel panel = new Panel();
		panel.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
		panel.setBounds(0,0,34,33);
		add("West", panel);
		
		Button edit = new Button();
		edit.setFont(new Font("Dialog", Font.PLAIN, 4));
		panel.add(edit);

		edit.addActionListener(
		    new ActionListener() {
		        public void actionPerformed(ActionEvent event) {
		            editor.setExpression(jome.getLinear());
		            editor.setVisible(true);
		        }
		    }
		);
		
		
		jome = new Jome();
		jome.setShiftX(10);
		jome.setDrawBounds(false);
		jome.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		jome.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		add("Center", jome);
		
		String expr = getParameter("expr");
				
		if (expr != null)
		    jome.setLinear(expr);
		    
		editor = new BasicEditor();
		editor.setVisible(false);
		editor.addEditorListener(this);
    }
    
    public void consumeEditorEvent(EditorEvent editorEvent) {
        String linear = (String) editorEvent.getArgument();
        jome.setLinear(linear);
        
        ListenableApplet parent = (ListenableApplet) getParent();
	    AppletEvent appletEvent = new AppletEvent(parent);
	    appletEvent.setAction(parent.getName(), this);
	    parent.fireAppletEvent(appletEvent);
    }
    
    /**
    * Sets the OpenMath object.
    * @param obj the OpenMath object.
    */
    public void setOpenMath(String openMath) {
        jome.setOpenMath(openMath);
    }
    
    /**
    * Returns the OpenMath object.
    */
    public String getOpenMath() {
        return jome.getOpenMath();
    }
}
